package com.example.baseappstore;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.example.baseappstore.databinding.ActivityLoginBinding;
import com.example.baseappstore.domain.request.LoginBean;
import com.example.baseappstore.ui.MainActivity2;
import com.example.baseappstore.utils.net.CommonUtils;
import com.example.baseappstore.utils.net.OkHttpUtils;
import com.example.baseappstore.utils.net.OkResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;
    private static final String[] PEERS = {
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private static final Integer REQUEST_CODE = 0x001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        requestPermission(PEERS);
        initEvent();
    }

    private void requestPermission(String[] peers) {
        ArrayList<String> strings = new ArrayList<>();
        for (String permission : peers) {
            // 检查权限
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                strings.add(permission);
            }
        }
        if (strings.size() != 0) {
            requestPermissions(strings.toArray(new String[0]), REQUEST_CODE);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            boolean allMatch = Arrays.stream(grantResults).allMatch(item -> item == PackageManager.PERMISSION_GRANTED);
            if (allMatch) {
                CommonUtils.showToast("权限申请全部通过");
            } else {
                Arrays.stream(permissions).forEach(per -> {
                    if (ContextCompat.checkSelfPermission(this, per) != PackageManager.PERMISSION_GRANTED) {
                        requestPermission(new String[]{per});
                    }

                });
            }
        }
    }

    private void initEvent() {
        binding.buttonLogin.setOnClickListener(v -> {
            doLogin();
        });
    }

    private void doLogin() {
        OkHttpUtils.doGet("/dev-api/common/province/page", new OkResult() {
            @Override
            public void onSuccess(String data) {
                System.out.println(data);
            }
        });
        HashMap<String, String> loginMap = new HashMap<>();
        loginMap.put("username", binding.editTextUsername.getText().toString().trim());
        loginMap.put("password", binding.editTextPassword.getText().toString().trim());
        OkHttpUtils.doPost("/dev-api/login", false, loginMap, data -> {
            LoginBean loginBean = CommonUtils.getGson().fromJson(data, LoginBean.class);
            OkHttpUtils.TOKEN = loginBean.getToken();
            startActivity(new Intent(this, MainActivity2.class));
        });
    }


}